package game.classes;

import game.Game;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.Vector;
import javax.swing.Timer;

/**
 *
 * @author Lean
 */
public class GameAI implements ActionListener {

    private GameMode mode;
    private Timer heart;
    private Vector shots;
    private Vector plutons;
    private Ship ship;
    private int difficulty;

    public GameAI(GameMode mode, int speed) {
        this.mode = mode;
        this.heart = new Timer(speed, this);
    }

    /**
     * Este es el metodo que llama el timer, aca pones todo lo que quieras
     * que se ejecute cada vez que el timer te de el tick de la vida (?)
     */
    public void actionPerformed(ActionEvent e) {
        moveShots();
        if (plutons.isEmpty()) {
            mode.notifyWin();
        } else {
            movePlutons();
        }
        detect();
        shoot();
        DeathZone();
        mode.notifyUpdate();
    }

    public void start() {
        heart.start();
    }

    public void stop() {
        heart.stop();
    }

    public void setDifficulty(int d) {
        difficulty = d;
    }

    public int getDifficulty() {
        return difficulty;
    }

    public void setShots(Vector shots) {
        this.shots = shots;
    }

    public void setPlutons(Vector plutons) {
        this.plutons = plutons;
        int x, y;
        x = 200;
        y = 50;
        for (int i = 0; i < 13; i++) {
            Vector v = new Vector();
            for (int j = 0; j < 4; j++) {
                Pluton p = new Pluton(j);
                p.setY(y);
                p.setX(x);
                v.add(p);
                y = y + Pluton.HEIGHT + 20;
            }
            y = 50;
            x = x +Pluton.HEIGHT + 20;
            this.plutons.add(v);
        }
        mode.notifyUpdate();
    }

    private void moveShots() {
        if (shots.size() > 0) {
            for (int i = 0; i < this.shots.size(); i++) {
                Shot s = (Shot) shots.get(i);
                if (s.getStep() < 0) {
                    if (s.getY() <= 0) {
                        shots.remove(s);
                    } else {
                        s.move();
                    }
                } else {
                    if (s.getY() >= Game.BHEIGHT) {
                        shots.remove(s);
                    } else {
                        s.move();
                    }
                }
            }

        }

    }

    private void movePlutons() {
        if (getStep()) {
            Vector v = (Vector) plutons.lastElement();
            Pluton p = (Pluton) v.get(0);
            if (p.getX() + Pluton.WIDTH > Game.BWIDTH) {
                movePlutonsY();
            } else {
                movePlutonsX();
            }
        } else {
            Vector v = (Vector) plutons.firstElement();
            Pluton p = (Pluton) v.get(0);
            if (p.getX() <= 0) {
                movePlutonsY();
            } else {
                movePlutonsX();
            }
        }

    }

    private boolean getStep() {
        Vector v = (Vector) plutons.get(0);
        Pluton p = (Pluton) v.get(0);
        if (p.getStep() > 0) {
            return true;
        } else {
            return false;
        }
    }

    private void movePlutonsY() {
        Vector v;
        Pluton p;
        for (int i = 0; i < plutons.size(); i++) {
            v = (Vector) plutons.get(i);
            for (int j = 0; j < v.size(); j++) {
                p = (Pluton) v.get(j);
                p.moveY();
                p.toggleStep();

            }
        }

    }

    private void movePlutonsX() {
        Vector v;
        Pluton p;
        for (int i = 0; i < plutons.size(); i++) {
            v = (Vector) plutons.get(i);
            for (int j = 0; j < v.size(); j++) {
                p = (Pluton) v.get(j);
                p.moveX();
            }
        }

    }

    private void detect() {
        for (int i = 0; i < shots.size(); i++) {
            Shot s = (Shot) shots.get(i);
            if (s.getStep() < 0) {
                detectF(s);
            } else {
                detectM(s);
            }



        }
    }

    private void detectF(Shot s) {
        Pluton p, d;
        Vector b = null;
        for (int j = 0; j < plutons.size(); j++) {
            Vector v = (Vector) plutons.get(j);
            p = (Pluton) v.get(0);
            if (s.getX() >= p.getX() && s.getX() <= p.getX() + Pluton.WIDTH) {
                b = v;
                break;
            }

        }
        d = null;
        if (b != null) {
            for (int j = 0; j < b.size(); j++) {
                p = (Pluton) b.get(j);
                Rectangle r = new Rectangle(s.getX(), s.getY(), Shot.WIDTH, Shot.HEIGHT);
                Rectangle r2 = new Rectangle(p.getX(), p.getY(), Pluton.WIDTH, Pluton.HEIGHT);
                if (r.intersects(r2)) {
                    shots.remove(s);
                    //  i=i-1;
                    mode.notifyImpact(p.getReward());
                    p.impacted();
                    b.remove(p);
                    if (b.isEmpty()) {
                        plutons.remove(b);
                    }

                    break;
                }

            }
        }
    }

    private void detectM(Shot s) {

        Rectangle r = new Rectangle(ship.getX(), ship.getY(), Ship.WIDTH, Ship.HEIGHT);
        Rectangle r2 = new Rectangle(s.getX(), s.getY(), Shot.WIDTH, Shot.HEIGHT);
        if (r.intersects(r2)) {
            mode.notifyCollide();
            shots.remove(s);
        }
    }

    private void shoot() {

        Random rand = new Random();
        int e = rand.nextInt(getDifficulty());
        if (e < 10) {
            e = rand.nextInt(plutons.size());
            Vector v = (Vector) plutons.get(e);
            e = rand.nextInt(v.size());
            Pluton p = (Pluton) v.get(e);
            p.fire(shots);
        }
    }

    public void setShip(Ship ship) {
        this.ship = ship;
    }

    private void DeathZone() // Un plutoniano se metio en tu espacio privado aka la banda horizontal donde te moves :D
    {
        Pluton p = getLastPluton();
        if (p.getY() + Pluton.HEIGHT >= ship.getY()) {
            mode.notifyDeath();
        }

    }

    private Pluton getLastPluton() {
        Pluton p = new Pluton(0);
        Vector v;
        for (int i = 0; i < plutons.size(); i++) {
            v = (Vector) plutons.get(i);
            for (int j = 0; j < v.size(); j++) {
                Pluton t = (Pluton) v.get(j);
                if ((p.getY() + Pluton.HEIGHT) < t.getY() + Pluton.HEIGHT) {
                    p = t;
                }

            }
        }
        return p;
    }
}
